

package com.jlb.base.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.jlb.core.SessionTools;
import com.jlb.core.enums.SessionEnum;
import com.jlb.core.tools.JwtToken;
import com.jlb.core.tools.StringTools;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.setting.dao.SettingDAO;
import com.jlb.setting.entity.Setting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by jiyan on 2017/8/25.
 */
public class AppBaseCtrl {
    protected final static Logger logger = LoggerFactory.getLogger(AppBaseCtrl.class);

    @Resource
    private SettingDAO settingDAO;

    @Resource
    protected RedisUtils redisUtils;

    /**
     * 从redis获取用的User
     */
    public JSONObject getUser(HttpServletRequest request) {
        String token = request.getHeader(SessionEnum.Authorization.name());
        if (StringTools.isEmpty(token)) {
            token = request.getParameter(SessionEnum.Authorization.name());
        }
        String sso_secret = settingDAO.loadByK(Setting.Key.Token_Secret.name()).getV();

        String account = JwtToken.getTokenValue(token, sso_secret, SessionEnum.Account.name());
        String sessionKey = SessionTools.Instance.genSessionKey(account);
        return (JSONObject) JSONObject.parse(redisUtils.get(sessionKey).toString());
    }

    /**
     * 从redis获取用的userCode
     */
    public String getUserCode(HttpServletRequest request) {
        return this.getUser(request).get("code").toString();
        //return  user != null ? user.getCode() : null;
    }


}
